﻿<?php
// +----------------------------------------------------------------------
// | ParseTorrent.class.php - MyPT
// +----------------------------------------------------------------------
// | Copyleft (c) 2010 By Asxzy All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Asxzy <asxzy@163.com>
// +----------------------------------------------------------------------

/*array info for ref:
announce
infohash
creation date
intenal name
torrentsize
filecount
announceruls
comment
filelist
*/


function ParseTorrent($filename) {
	require_once("BDecode.class.php") ;
	require_once("BEncode.class.php") ;



	//check file type is a torrent
	//$torrent = explode(".", $filename);
    //$fileend = end($torrent);
    //$fileend = strtolower($fileend);

	$torrent = new BDECODE($filename);
	$TorrentInfo = array();
	global $array;
	$array = $torrent->result;
	if(isset($array['error'])) {
		return $array;
	}
	if ($array === FALSE){
		$array['error'] = ('Parser Error: Error Opening torrent, unable to decode');
	}else{
		if(array_key_exists("info", $array) === FALSE){
			$array['error'] = ('Error Opening torrent');
		}else{
			//Get Announce URL
			$TorrentInfo['announce'] = $array["announce"];
				//Get Announce List Array
			if (isset($array["announce-list"])){
				$TorrentInfo['announce-list'] = $array["announce-list"];
			}
				//Read info, store as (infovariable)
			$infovariable = $array["info"];
			
			// Calculates SHA1 Hash
			$infohash = sha1(BEncode($infovariable));
			$TorrentInfo['infohash'] = $infohash ;
			
			// Calculates date from UNIX Epoch
			$makedate = date('r' , $array["creation date"]);
			$TorrentInfo['makedate'] = $makedate ;
				// The name of the torrent is different to the file name
			$TorrentInfo['name'] = $infovariable['name'] ;
				//Get File List
			if (isset($infovariable["files"]))  {
				// Multi File Torrent
				$filecount = "";
					//Get filenames here
				$TorrentInfo[files] = $infovariable["files"];
					foreach ($infovariable["files"] as $file) {
					$filecount += "1";
					$multiname = $file['path'];//Not needed here really
					$multitorrentsize = $file['length'];
					$torrentsize += $file['length'];
				}
					$TorrentInfo['size'] = $torrentsize;  //Add all parts sizes to get total
				$TorrentInfo['numfiles'] = $filecount;  //Get file count
			}else {
				// Single File Torrent
				$torrentsize = $infovariable['length'];
				$TorrentInfo['size'] = $torrentsize;//Get file count
				$TorrentInfo['numfiles'] = "1";
				$TorrentInfo['private'] = $array['info']['private'];
			}
				// Get Torrent Comment
			if(isset($array['comment'])) {
				 $TorrentInfo['comment'] = $array['comment'];
			}
		}
	}
	return $TorrentInfo;
}//End Function
?>